package com.example.demo.controller;

import com.example.demo.entity.CodeAssociation;
import com.example.demo.entity.CodingRules;
import com.example.demo.entity.MaintenanceRecord;
import com.example.demo.service.CodeAssignmentRulesService;
import com.example.demo.service.CodingRulesService;
import com.example.demo.util.GetMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/coding")
@RequiredArgsConstructor
@Component
public class CodingRulesController {
    private final CodingRulesService codingRulesService;
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @PostMapping("/insert")
    public GetMessage insert (String userId, @RequestBody CodingRules codingRules){
        //String userId=token;
        codingRules.setUserid(userId);
        boolean bol=codingRulesService.save(codingRules);
        if(bol){
            return new GetMessage("成功","200",codingRules);
        }
        return new GetMessage("失败","500",null);
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @PostMapping("/update")
    public GetMessage update (String userId, @RequestBody CodingRules codingRules){
        //String userId=token;

        boolean bol=codingRulesService.updateById(codingRules);
        if(bol){
            return new GetMessage("成功","200",codingRules);
        }
        return new GetMessage("失败","500",null);
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/select")
    public GetMessage select (String userId, String appId,String trainingId){
        //String userId=token;
        List<CodingRules> list =codingRulesService.select(userId,appId,trainingId);
        return new GetMessage("成功","200",list);
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/delete")
    public GetMessage delete (String userId,Integer id){
        Integer i =codingRulesService.deleteById(id);
        return new GetMessage("成功","200",i);
    }
}
